/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.controller.impl;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.myontology.controller.AbstractController;
import org.myontology.controller.EditMode;
import org.myontology.controller.Selector;
import org.myontology.controller.SesameSession;
import org.myontology.controller.SetStringToRequestAction;
import org.myontology.controller.Update;
import org.myontology.controller.pages.BasicPage;
import org.myontology.controller.pages.RedirectPage;
import org.myontology.controller.pages.UserRedirectPage;
import org.myontology.controller.pages.XMLPage;
import org.myontology.controller.selectors.Admin;
import org.myontology.controller.selectors.Logged;
import org.myontology.controller.selectors.Parameter;
import org.myontology.specialpages.NavigationAction;
import org.myontology.specialpages.SpecialPageAction;
import org.myontology.specialpages.SpecialPagesListAction;
import org.myontology.specialpages.dynamic.DSPList;
import org.myontology.specialpages.dynamic.LoadDynamicPage;
import org.myontology.specialpages.dynamic.NewDynamicPage;
import org.myontology.specialpages.dynamic.SaveDynamicPage;
import org.myontology.specialpages.dynamic.TestQuery;
import org.myontology.users.EditUser;
import org.myontology.users.ExportUsersAction;
import org.myontology.users.ImportUsersAction;
import org.myontology.users.Login;
import org.myontology.users.Logout;
import org.myontology.users.NewUser;
import org.myontology.users.OpenIDLogin;
import org.myontology.users.OpenIDPage;
import org.myontology.users.OpenIDVerify;
import org.myontology.users.SaveUser;
import org.myontology.view.items.FormBuilder;


/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 */
public class MyOntologyController extends AbstractController {

	static {
		Logger.getLogger(SesameSession.class).setLevel(Level.DEBUG);
	}
	/* (non-Javadoc)
	 * @see org.myontology.controller.AbstractController#init()
	 */
	@Override
	public void init() {
		
		
		
		FormBuilder.setDictionary(new MyComponentDictionary());
		
		dIController = new MyOntologyDIController();
		

		addSelector(Parameter.IsAction("index").onSelection(new CloudAction(), null));
		
		
		Selector loggedIn = Logged.In();

		addSelector(loggedIn);
		
		Selector invite = loggedIn.otherwise(
				Parameter.IsAction("invite"));
		loggedIn.otherwise(Parameter.IsAction("changeAdmin").onSelection(new CreateAdmin(), new RedirectPage("/index.do")));
		loggedIn.otherwise(Parameter.IsAction("login").onSelection(new Login(), new UserRedirectPage("content","components/login/login.jsp")));
		loggedIn.otherwise(Parameter.IsAction("confirm").onSelection(new Confirm(), new UserRedirectPage("content","components/login/confirm.jsp")));
		loggedIn.otherwise(Parameter.IsAction("openid_login").onSelection(new OpenIDLogin(), new OpenIDPage()));
		loggedIn.otherwise(Parameter.IsAction("openid_verify").onSelection(new OpenIDVerify(), new BasicPage("content","components/login/new_user.jsp")));
		addSelector(Parameter.IsAction("save_user").onSelection(new SaveUser(), new BasicPage("content","components/login/new_user.jsp")));
		
		addSelector(Parameter.IsAction("editMode").onSelection(new EditMode(), new XMLPage("content","components/editMode.jsp")));
		addSelector(Parameter.IsAction("viewMode").onSelection(new EditMode(), new XMLPage("content","components/viewMode.jsp")));
		addSelector(Parameter.IsAction("update").onSelection(new Update(), new XMLPage("content","components/viewMode.jsp")));
		
		loggedIn.and(Parameter.IsAction("edit_user").onSelection(new EditUser(), new BasicPage("content","components/login/new_user.jsp")));
		loggedIn.otherwise(Parameter.IsAction("register").onSelection(new NewUser(), new BasicPage("content","components/login/new_user.jsp")));
		loggedIn.and(Parameter.IsAction("logout").onSelection(new Logout(), new UserRedirectPage()));
		
		addSelector(Parameter.IsAction("showclass").onSelection(new ShowAction(), new BasicPage("content","components/classcomponents/showClass.jsp")));
		addSelector(Parameter.IsAction("showOM").onSelection(new ShowAction(), new BasicPage("content","components/ontologymodulecomponents/showOntologyModule.jsp")));
		addSelector(Parameter.IsAction("showproperty").onSelection(new ShowAction(), new BasicPage("content","components/propertycomponents/showProperty.jsp")));
		
		
		addSelector(Parameter.IsAction("userDiscussion").onSelection(new DiscussionAction(), new BasicPage("content","components/discussion/discussion.jsp")));
		addSelector(Parameter.IsAction("discussionAdd").onSelection(new DiscussionAddAction(), new BasicPage("content","components/discussion/discussion.jsp")));
		
		addSelector(Parameter.IsAction("newFreezepoint").onSelection(new EmptyAction(), new BasicPage("content","components/freezepoints/add.jsp")));
		addSelector(Parameter.IsAction("showAllFreezepoints").onSelection(new EmptyAction(), new BasicPage("content","components/freezepoints/overview.jsp")));
		
		addSelector(Parameter.IsAction("changerequests").onSelection(new EmptyAction(), new BasicPage("content","components/changerequest/showAllChangerequests.jsp")));
		addSelector(Parameter.IsAction("new_changerequest").onSelection(new EmptyAction(), new BasicPage("content","components/changerequest/showChangerequestForm.jsp")));
		addSelector(Parameter.IsAction("new_userdiscussion").onSelection(new EmptyAction(), new BasicPage("content","components/discussion/showUserdiscussionForm.jsp")));
		addSelector(Parameter.IsAction("show_changerequest").onSelection(new EmptyAction(), new BasicPage("content","components/changerequest/showChangerequest.jsp")));
		
		
		addSelector(Parameter.IsAction("tag").onSelection(new EmptyAction(), new BasicPage("content","components/tagcomponents/showTagElements.jsp")));
		addSelector(Parameter.IsAction("showversions").onSelection(new EmptyAction(), new BasicPage("content","components/versions/showAllVersions.jsp")));
		
		addSelector(Parameter.IsAction("search").onSelection(new SearchAction(), new BasicPage("content","components/search/search.jsp")));
		loggedIn.and((Parameter.IsAction("undo").onSelection(new HistoryPreviewAction(), new BasicPage())));
		
		
		addSelector(Parameter.IsAction("importUsers"))
		.onSelection(new ImportUsersAction(), new BasicPage("content","components/userManagement/importUser.jsp"));
		
		
		onInvite(invite);
		onLoggedIn(loggedIn);
		dynamicSpecialPages(loggedIn);
		
		
		admin(addSelector(new Admin()));
	}

	
	private void admin(Selector admin) {
		
		admin.and(Parameter.IsAction("deleteOM")
				.onSelection(new DeleteOMAction(), new BasicPage("content","components/ontologymodulecomponents/selectOntologyModule.jsp")));
		admin.and(Parameter.IsAction("lock")
				.onSelection(new ShowAction(), new BasicPage("content","components/ontologymodulecomponents/showOntologyModule.jsp")));
		admin.and(Parameter.IsAction("exportUsers"))
				.onSelection(new ExportUsersAction(), new BasicPage("content","components/userManagement/exportUser.jsp"));
	}
	
	private void onLoggedIn(Selector loggedIn) {
		
		loggedIn.and(Parameter.IsAction("newProperty").onSelection(new HideAction(), new BasicPage("content","components/propertycomponents/showPropertyForm.jsp")));
		loggedIn.and(Parameter.IsAction("newClass").onSelection(new HideAction(), new BasicPage("content","components/classcomponents/showClassForm.jsp")));
		loggedIn.and(Parameter.IsAction("newOntologyModule").onSelection(new HideAction(), new BasicPage("content","components/ontologymodulecomponents/showOntologyModuleForm.jsp")));
		
		
		loggedIn.and(Parameter.IsAction("specialPages")
				.onSelection(
						new SpecialPagesListAction(), new BasicPage("content","components/specialPages/pageList.jsp"))
				);
		
		loggedIn.and(Parameter.IsAction("deleteTourism")
				.onSelection(
						new DeleteTourismAction(), new BasicPage("content","components/specialPages/pageList.jsp"))
				);
		
		loggedIn.and(Parameter.IsAction("specialPage")
			.onSelection(
					new SpecialPageAction(), new BasicPage("content","components/specialPages/page.jsp"))
					);
		loggedIn.and(Parameter.IsAction("navigate")
				.onSelection(
						new NavigationAction(), new XMLPage("content","components/specialPages/page.jsp"))
						);
		loggedIn.and(Parameter.IsAction("OWLimport")
				.onSelection(new OWLImportAction(), new BasicPage("content","components/import/start.jsp")));
		loggedIn.and(Parameter.IsAction("OWLimportError")
				.onSelection(new OWLImportErrorAction(), new BasicPage("content","components/import/error.jsp")));
		loggedIn.and(Parameter.IsAction("OWLimportValidate")
				.onSelection(new OWLImportValidateAction(), new BasicPage("content","components/import/validate.jsp")));
		loggedIn.and(Parameter.IsAction("OWLrealImport")
				.onSelection(new OWLImportValidateAction(), new BasicPage("content","components/import/import.jsp")));
	}
	
	private void dynamicSpecialPages(Selector sel) {
		sel.and(Parameter.IsAction("dynamicSpecialPages")
				.onSelection(
						new DSPList(), new BasicPage("content","components/specialPages/dynamicPagesList.jsp"))
				);
		sel.and(Parameter.IsAction("loadDynamicPage")
				.onSelection(
						new LoadDynamicPage(), new BasicPage("content","components/specialPages/editDynamicPage.jsp"))
				);
		sel.and(Parameter.IsAction("saveDynamicPage")
				.onSelection(
						new SaveDynamicPage(), new BasicPage("content","components/specialPages/editDynamicPage.jsp"))
				);
		sel.and(Parameter.IsAction("newDynamicPage")
				.onSelection(
						new NewDynamicPage(), new BasicPage("content","components/specialPages/editDynamicPage.jsp"))
				);
		sel.and(Parameter.IsAction("testQuery")
				.onSelection(new TestQuery(), new XMLPage("content","components/specialPages/page.jsp")));
	}
	
	private void onInvite(Selector invite) {
		Selector submit = Parameter.Has("submit")
		.onSelection(
				new SetStringToRequestAction("invite","submit"), null);
		invite.and(submit);
		
		invite.otherwise().onSelection(
				new SetStringToRequestAction("invite","noInvite"), null);
	
		submit.otherwise().onSelection(
				new SetStringToRequestAction("invite","noSubmit"), null);
	}
	
}
